﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YXQ.Service;
using YXQ.Dao.ViewModel;
using System.Text;

using YXQ.Common.Extension;
namespace YXQ.Manager.Manager
{
    public partial class Index : BaseAdminPage
    {
        public string strMenu = string.Empty;
        List<MudleModel> mens = new List<MudleModel>();
        StringBuilder sb = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (AdminUser != null) {
                    ltrName.Text = AdminUser.UserName;
                }
                LoadMenus();
                LoadMsg();
            }
        }

        private void LoadMsg()
        {
            int recor=0;
          List<MessageModel> mdls=  CenterService.messageService.GetListModel("", 0, 10, out recor);
          repQu.DataSource = mdls;
          repQu.DataBind();
        }

        private void LoadMenus()
        {
            GetMenus(0);
            List<Menus> mes = mens.Select(s => new Menus()
            {
                menuname = s.Title,
                menuid = s.Id,
                icon = "icon-nav",
                url = s.PageUrl,
                menus = s.SonModels.Select(m => new Menus() { icon = "icon-nav", menuname = m.Title, menuid = m.Id, url = m.PageUrl }).ToList()
            }).ToList();
            strMenu = sb.Append(mes.ToJsonItem()).ToString();
        }



        public void GetMenus(int pid)
        {
            
            List<MudleModel> models = CenterService.mudleService.GetListByPid(pid);
          
            foreach (var mdl in models)
            {

                var sonmdl= GetSonMenus(mdl.Id);
                //超级管理员默认所有权限
                if (AdminUser != null)
                {
                    if (AdminUser.UserName.ToLower().Equals("admin"))
                    {
                        mdl.SonModels = sonmdl;
                        mens.Add(mdl);
                    }

                    //一般用户权限
                    if (AdminUser.UserSysRight != null)
                    {
                        if (AdminUser.UserSysRight.IndexOf(mdl.FunCode) >= 0)
                        {
                            List<MudleModel> mudson = new List<MudleModel>();
                            foreach (var s in sonmdl) {
                                if (AdminUser.UserSysRight.IndexOf(s.FunCode) >= 0) {
                                    mudson.Add(s);
                                }
                            }
                            mdl.SonModels = mudson;
                            mens.Add(mdl);
                        }
                    }
                }


            }
          


        }



        private List<MudleModel> GetSonMenus(int p)
        {
            List<MudleModel> models = CenterService.mudleService.GetListByPid(p);
           
            return models;
        }
    }
}